<?
class Fit_Change{
    
    /*
     * 角色转换，散客转换成分销商并建立分销关系
     * $memberID 散客ID
     * $apply_did 分销商ID
     **/
    public static function role_transition($memberID,$apply_did){
        $account = $_SESSION['account'];
        // 散客转换分销商身份
        $sql="select dtype from pft_member where id=$memberID and account='$account' limit 1";
        $GLOBALS['le']->query($sql);
        $GLOBALS['le']->fetch_assoc();
        if($GLOBALS['le']->f('dtype')==5){
            $sql="update pft_member set dtype=1 where id=$memberID and account='$account' limit 1";
            $GLOBALS['le']->query($sql);
        }
        
        // 建立关系
        $check_sql = "select id from pft_member_relationship where parent_id=$apply_did and son_id=$memberID and ship_type=0 limit 1";
        $GLOBALS['le']->query($check_sql);
        if(!$GLOBALS['le']->fetch_assoc()) {
            $ins = "insert into pft_member_relationship (parent_id,son_id,son_id_type,ship_type,rectime) values ($apply_did,$memberID,0,0,now())";
            return $GLOBALS['le']->query($ins);
        }
        return true;
    }

    /*
     * 设置购买权限
     * $pid 产品 id
     * $aid 上上级供应商
     * $mid 上级供应商
     $ $memberID 分销商 ID
     **/   
    public static function setBuyPermissions($pid,$aid,$mid,$memberID){

        if($aid==$mid){// 自己供应
            $check_sql = "select id,pids from pft_product_sale_list where fid=$memberID and aid=$aid limit 1";
            $GLOBALS['le']->query($check_sql);
            $GLOBALS['le']->fetch_assoc();
            $list_id = $GLOBALS['le']->f('id');
            if($list_id){
                if($GLOBALS['le']->f('pids')!='A'){
                    $sql = "update pft_product_sale_list set pids='A' where id=$list_id limit 1";
                    $GLOBALS['le']->query($sql);
                    // echo $sql;
                }
            }else{
                $sql = "insert into pft_product_sale_list set pids='A',fid=$memberID,aid=$aid";
                $GLOBALS['le']->query($sql);
                // echo $sql;
            }
        }else{ // 转分销
            // 一手
            $check_sql="select pids from pft_product_sale_list where fid=$mid and aid=$aid limit 1";
            $GLOBALS['le']->query($check_sql);
            $GLOBALS['le']->fetch_assoc();
            $pids = $GLOBALS['le']->f('pids');
            if($pids) $pids = explode(',',$pids);
            if($pids && in_array($pid,$pids)){

                $check_sql = "select id,status from pft_p_apply_evolute where pid=$pid and fid=$memberID and sid=$mid limit 1";
                $GLOBALS['le']->query($check_sql);
                if(($tmp = $GLOBALS['le']->fetch_assoc())){
                    $id=$tmp['id'];
                    if($tmp['status'] == 1){ // 新转换的分销商已经权限
                        $upd_sql="update pft_p_apply_evolute set status=0 where id=$id and pid=$pid and fid=$memberID and sid=$mid limit 1";
                        $GLOBALS['le']->query($upd_sql);
                    }
                }else{
                    $sql="insert into pft_p_apply_evolute set pid=$pid,fid=$memberID,sid=$mid,sourceid=$aid,aids='$aid,$mid',lvl=1,active=0";
                    $GLOBALS['le']->query($sql);
                }
            }else{
                // 以下都是二手供应
                $sql_get = "select id,aids,sid,sourceid,lvl,active from pft_p_apply_evolute where pid=$pid and sid=$aid and fid=$mid and status=0 limit 1";
                $GLOBALS['le']->query($sql_get);
                if(($db_data = $GLOBALS['le']->fetch_assoc())){
                    $id = $db_data['id'];
                    if($db_data['active']==0){
                        // 开启转供应
                        $upd_sql="update pft_p_apply_evolute set active=1 where pid=$pid and sid=$aid and fid=$mid limit 1";
                        $GLOBALS['le']->query($upd_sql);
                        
                    }
                    
                    $check_sql = "select id,status from pft_p_apply_evolute where pid=$pid and fid=$memberID and sid=$mid limit 1";
                    
                    $GLOBALS['le']->query($check_sql);
                    if(($tmp = $GLOBALS['le']->fetch_assoc())){
                        if($tmp['status'] == 1){ // 新转换的分销商已经权限
                            $upd_sql="update pft_p_apply_evolute set status=0 where id=$id and pid=$pid and fid=$memberID and sid=$mid limit 1";
                            $GLOBALS['le']->query($upd_sql);
                        }
                    }else{
                        $aids_n  = $db_data['aids'].",$mid";
                        $lvl_n = $db_data['lvl']+1;
                        $sourceid = $db_data['sourceid'];
                        $sql="insert into pft_p_apply_evolute set pid=$pid,fid=$memberID,sid=$mid,sourceid=$sourceid,aids='$aids_n',lvl=$lvl_n,active=0";
                        $GLOBALS['le']->query($sql);
                    }
                }
            }
        }
    }
}
?>